import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

@Component({
  selector: 'sa-scheduling',
  templateUrl: './scheduling.component.html',
})
export class SchedulingComponent implements OnInit {
	tableData:any[] = []; //table数据
	selected:any[] = []; //row选择的数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword

	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage == undefined? 1:queryParams.nowPage;
		});
	}

	ngOnInit() {
		this.load();
	}
	//表格搜索
	table_search(word) {
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
    refresh(){
    	this.load();
    }
	//初始化加载list数据
	load() {
		let reqdata = 'gridPager={"pageSize":' + this.pageSize + ',"nowPage":' + this.curPage + ',"parameters":{"searchName":"' + this.searchKey + '"}}'
		this.jsonApiService.http("job/list.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//执行,禁用事件
	updateStatus(data,type,mes){
		this.jsonApiService.confirm_message('确认要'+mes+'该任务?').then(()=>{
			if(type === 'start'){
				let reqdata = 'id=' + data.id;
				this.jsonApiService.http("job/runNow.html", reqdata).subscribe(data => {
					this.jsonApiService.message(data.message);
					if(data.success){
						this.load();
					}
				})
			}else if(type === 'open'||type === 'stop'){
				var jobStatus = '0';
				if(type === 'stop'){
					jobStatus = '0';
				}else if(type ==='open'){
					jobStatus = '1';
				}
				let reqdata = 'id=' + data.id+'&status='+jobStatus;
				this.jsonApiService.http("job/updateStatus.html", reqdata).subscribe(data => {
					this.jsonApiService.message(data.message);
					if(data.success){
						this.load();
					}
				})
			}
		})
	}
	//调度配置
	config_scheduling(row){
		this.router.navigate(['/system-config/scheduling/scheduling-config'], {queryParams: {id:row.id,type: 'config',nowPage:this.curPage}});
	}
	//新建调度任务
	new_scheduling(){
		this.router.navigate(['/system-config/scheduling/scheduling-page'], {queryParams: {type: 'new'}});
	}
	//新建/编辑页面
	updata_scheduling(row) {
		this.router.navigate(['/system-config/scheduling/scheduling-page'], {queryParams: {id: row.id,type: 'updata',nowPage:this.curPage}});
	}
	//删除调度
	del_scheduling(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'ids=' + row.id;
			this.jsonApiService.http("job/deleteBatch.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
}